//
//  CustomKeyBoard.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/8/7.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class CustomKeyBoard: UIView {

    
    var kbCollectionView:UICollectionView!
    var effect:EffectView!
    var shadowLayer:CALayer!
    
    var contentView:UIView!
    
    var textField:UITextField!
    
    var backInputValue:BackStringValue!
    
    let title = ["1","2","3","4","5","6","7","8","9","取消","0","确定",]
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.effect = EffectView.init(frame: frame)
        self.effect.alpha = 0
        self.addSubview(self.effect)
        self.effect.hidenClosure = { [weak self]  in
            self?.hiden()
        }
        
        self.initContent()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initContent(){
        self.contentView = UIView.init(frame: CGRect.init(x: 0, y: self.height, width: self.width, height: 320 + (Utils.isIphoneX() ? 34 : 0)))
        self.contentView.backgroundColor = UIColor.clear
        self.addSubview(self.contentView)
        
        shadowLayer = CALayer.init()
        shadowLayer.frame = CGRect.init(x: 0, y: 10 , width: self.width, height: self.contentView.height)//- (Utils.isIphoneX() ? 34 : 0) - 310
        shadowLayer.cornerRadius = 20
        shadowLayer.backgroundColor = UIColor.white.cgColor
        shadowLayer.shadowColor = UIColor.init(hexColor: "#000000").cgColor
        shadowLayer.shadowOffset = CGSize.init(width: 0, height: -10)
        shadowLayer.shadowRadius = 5
        shadowLayer.shadowOpacity = 0.08
        self.contentView.layer.addSublayer(shadowLayer)
        
        self.textField = UITextField.init(frame: CGRect.init(x: 12, y: 10, width: self.width - 62, height: 65))
        self.textField.placeholder = "请输入充值金额，至少100"
        self.textField.isUserInteractionEnabled = false
        self.textField.font = UIFont.systemFont(ofSize: 18)
        self.textField.addTarget(self, action: #selector(valueChange(textField:)), for: .editingChanged)
        self.contentView.addSubview(self.textField)
        Utils.setTextFieldPlaceholderColor(textField: self.textField)
        
        let cleanBtn = UIButton.init(type: .custom)
        cleanBtn.frame = CGRect.init(x: self.width - 45, y: 10, width: 50, height: 65)
        cleanBtn.setImage(UIImage.init(named: "icon_delete_4"), for: .normal)
        cleanBtn.addTarget(self, action: #selector(cleanBtnClick), for: .touchUpInside)
        self.contentView.addSubview(cleanBtn)
        
        let flowlayout = UICollectionViewFlowLayout()
        flowlayout.itemSize = CGSize.init(width: (UIScreen.ScreenWidth - 45)/3, height: 50)
        flowlayout.sectionInset = UIEdgeInsetsMake(0, 12, 15, 12)
        flowlayout.minimumLineSpacing = 8
        flowlayout.minimumInteritemSpacing = 10
        
        self.kbCollectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: self.textField.bottom, width: UIScreen.ScreenWidth, height: self.contentView.height - 75 - (Utils.isIphoneX() ? 34 : 0)), collectionViewLayout: flowlayout)
//        self.kbCollectionView.bounces = false
        self.kbCollectionView.delegate = self
        self.kbCollectionView.dataSource = self
        self.kbCollectionView.backgroundColor = UIColor.nav_color
        self.kbCollectionView.register(CustomKbCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "kbCell")
        self.contentView.addSubview(self.kbCollectionView)
        
    }
    
    @objc func valueChange(textField:UITextField){
        
    }
    
    @objc func cleanBtnClick(){
        self.textField.text = nil
    }
    
    func show(){
        UIView.animate(withDuration: 0.3) {
            self.contentView.top = self.height - (Utils.isIphoneX() ? 34 : 0) - 310
            self.effect.alpha = 1
        }
    }
    
    func hiden(){
        
        UIView.animate(withDuration: 0.2, animations: {
            self.contentView.top = self.height
        }) { (_) in
            self.isHidden = true
        }
    }
}


extension CustomKeyBoard:UICollectionViewDelegate,UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
         return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "kbCell", for: indexPath) as? CustomKbCollectionViewCell
        cell!.titleButton.setTitle(self.title[indexPath.row], for: .normal)
        if indexPath.row == 9 || indexPath.row == 11 {
            cell?.titleButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        }else{
            cell?.titleButton.titleLabel?.font = UIFont.systemFont(ofSize: 23)
        }
        
        cell?.backClosure = { [weak self] (value) in
            if value == "取消" {
                self?.hiden()
                return
            }else if value == "确定" {
                guard self?.textField.text != "" else {
                    return
                }
                if self?.backInputValue != nil {
                    self?.backInputValue((self?.textField.text)!)
                    self?.hiden()
                }
            }else{
                self?.textField.text = (self?.textField.text)! + value
            }
        }
        return cell!
        
    }
    
}



















